//
//  UIButtonExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/7/29.
//

import UIKit
import Kingfisher

class UIButtonExtension: NSObject {

}

extension UIButton {
    func buttonConfig(normalTitle: String = "", normalImageNamed: String = "", normalTitleColor: UIColor = UIColor.black, titleFont: UIFont, backgroundColor: UIColor = UIColor.clear) {
        if normalTitle.count > 0 {
            self.setTitle(normalTitle, for: UIControl.State.normal)
        }
        self.setTitleColor(normalTitleColor, for: UIControl.State.normal)
        if normalImageNamed.count > 0 {
            self.setImage(UIImage.init(named: normalImageNamed)?.withRenderingMode(.alwaysOriginal), for: .normal)
        }
        if normalTitle.count > 0 && normalImageNamed.count > 0 {
            self.setTitle(normalTitle, for: UIControl.State.normal)
            self.setImage(UIImage.init(named: normalImageNamed), for: .normal)
        }
        self.titleLabel?.font = titleFont
        self.backgroundColor = backgroundColor
    }
    
    func noHighLight() {
        self.adjustsImageWhenDisabled = false
        self.adjustsImageWhenHighlighted = false
    }
    
    func setImageEdge(edge: UIEdgeInsets) {
        self.imageEdgeInsets = edge
        self.imageView?.contentMode = .scaleAspectFill
        self.contentVerticalAlignment  = .fill
        self.contentHorizontalAlignment  = .fill
    }
    
    func insetCircleView(circleWidth: CGFloat, borderColor: UIColor) {
        let circleView = UIView()
        self.addSubview(circleView)
        circleView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize.init(width: circleWidth, height: circleWidth))
        }
        circleView.tintColor = UIColor.clear
        circleView.layer.borderWidth = 1
        circleView.layer.cornerRadius = circleWidth/2
        circleView.layer.borderColor = borderColor.cgColor
        circleView.isUserInteractionEnabled = false
    }
    
    func autoDownloadImage(_ imageKey: String?) {
        
        if imageKey?.hasPrefix("http") ?? false  {
            
            if let cacheImage = ImageCache.default.retrieveImageInMemoryCache(forKey: imageKey ?? "jd") {
                self.setImage(cacheImage, for: .normal)
            } else {
                self.kf.setImage(with: URL(string: (imageKey ?? "")), for: .normal)
            }

        } else {
            
            self.downloadImage(imageKey ?? "")
        }
    }
    
    func downloadImage(_ imageKey: String) {
        var params = [String: Any]()
        params["imageKey"] = imageKey

        JDDataNetWorkRequest(.downloadImage(params)) { responseData in
            printLog("获取图片成功:\(responseData)")
            if responseData.count == 0 {
                self.setImage(UIImage(named: "loading_placeholder")?.withRenderingMode(.alwaysOriginal), for: .normal)
                self.backgroundColor = .commonColor()
                return;
            }
            let dataImage = UIImage(data: responseData)
//            self.image = dataImage
            self.setImage(dataImage?.withRenderingMode(.alwaysOriginal), for: .normal)
        } failure: { error in
            printLog("获取图片失败")
        } error: { errorCode in
            printLog("获取图片错误Code:\(errorCode)")
        }
    }
    
}
